package com.nomito.minimito.externalUtils.tyfUtils;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


import java.io.IOException;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author NovaTian
 * @date 2020/11/7/16:02
 */
public class OkHttpUtil {

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    /**
     * 请求token的接口地址
     */
    private final static String URL = "https://aip.baidubce.com/oauth/2.0/token";
    /**
     * 请求token的AK
     */
    private final static String AK = "guoIEfzlEUlzUxTCOUdipGW1";
    /**
     * 请求token的SK
     */
    private final static String SK = "4kVH1TpWMkukD6uvaRFhUusediTELTPa";

    /**
     * 获得token
     *
     * @return
     */
    public static String getToken() {
        String access_token = "";
        try {
            access_token += "try";
            OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = new FormBody.Builder()
                    .add("grant_type", "client_credentials")
                    .add("client_id", AK)
                    .add("client_secret", SK)
                    .build();
            Request request = new Request.Builder()
                    .url(URL)
                    .post(requestBody)
                    .build();
            Response response = null;
            response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                String data = response.body().string();
                JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject();
                access_token = jsonObject.get("access_token").getAsString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return access_token;
        }
    }

    /**
     * post基础方法
     * @param url
     * @param map
     * @return
     */
    public static String postResult(String url, Map<String, Object> map) {
        String dataJson = "";
        FormBody.Builder builder = new FormBody.Builder();
        if (map != null) {
            for (String key : map.keySet()) {
                builder.add(key, (String) map.get(key));
            }
        }
        try {
            OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = builder.build();
            Request request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .build();
            Response response = null;
            response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                dataJson = response.body().string();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataJson;
    }

    /**
     * 从json string得到返回结果
     *
     * @param url
     * @param json
     * @return
     */
    public static String postResultFromJsonString(String url, String json) {
        String jsonString = getJsonString(url, json);
        return jsonString;
    }

    /**
     * 从对象得到返回结果
     *
     * @param url
     * @param object
     * @return
     */
    public static String postResultFromObject(String url, Object object) {
        Gson gson = new Gson();
        String jsonString = gson.toJson(object);
        System.out.println("jsonString = " + jsonString);
        return getJsonString(url, jsonString);
    }

    /**
     * 请求通用方法
     *
     * @param url
     * @param json
     * @return
     */
    private static String getJsonString(String url, String json) {
        String dataJson = null;
        try {
            OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = RequestBody.create(JSON, json);
            Request request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .build();
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                dataJson = response.body().string();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataJson;
    }
}
